<?php

namespace App\Http\Controllers\Admin;

use Carbon\Carbon;
use App\Http\Controllers\BaseController;
use Illuminate\Http\Request;
use App\Exceptions\BasicException;
use App\Response\Response;

class RoomContributionConfigController extends BaseController
{
    /**
     * @api {get} room/contribution/config 房间贡献值配置-列表
     * @apiVersion 0.1.0
     * @apiName 房间贡献值配置列表
     * @apiGroup 房间贡献值
     * @apiSuccessExample 成功响应
     * {
     * "code": 200,
     * "data": [
     * {
     * "id": 1,
     * "level 等级": 1,
     * "name 名称": "等级1",
     * "value 贡献值": 100,
     * "icon 图标": "http://yutangyuyin1.oss-cn-hangzhou.aliyuncs.com/newadmin/e78dcbe611658691758b9f789a821aadKxXca.png",
     * "add_time 添加时间": "2021-07-13 11:42:29",
     * "update_time 更新时间": ""
     * }
     * ]
     * }
     */
    public function list()
    {
        $list = app('model_room_contribution_config')->orderBy('level', 'asc')->get()->map(function ($value) {
            $value->add_time = date('Y-m-d H:i:s', $value->add_time);
            $value->update_time = $value->update_time > 0 ? date('Y-m-d H:i:s', $value->update_time) : '';

            return $value;
        });

        return Response::sendData($list);
    }

    /**
     * @api {post} room/contribution/config 房间贡献值配置-添加
     * @apiVersion 0.1.0
     * @apiName 添加房间贡献值配置
     * @apiGroup 房间贡献值
     * @apiParam {number} level 等级,必传
     * @apiParam {string} name 名称,必传
     * @apiParam {string} icon 图标,必传
     * @apiParam {number} value 贡献值,必传
     * @apiSuccessExample 成功响应
     * {
     * "code": 200,
     * "data": true
     * }
     */
    public function add(Request $request)
    {
        //参数校验
        $v = app('validator')->make(
            $request->all(),
            [
                'level' => 'required|numeric',
                'name'  => 'required',
                'icon'  => 'required',
                'value' => 'required|numeric',
            ],
            [
                'level.required' => '等级不能为空',
                'name.required'  => '名称不能为空',
                'level.exists'   => '等级已存在',
                'icon.exists'    => '图标不能为空',
                'value.numeric'  => '贡献值必须为数字',
            ]);
        if ($v->fails()) {
            throw new BasicException(10001, $v->errors()->first());
        }
        $data = $request->only(['level', 'name', 'icon', 'value']);
        $data['add_time'] = Carbon::now()->timestamp;
        if (app('model_room_contribution_config')->where('level', $data['level'])->count() > 0) {
            throw new BasicException(0, '等级已存在');
        }

        app('model_room_contribution_config')->insert($data);

        app('redis')->del('room:contribution:config');

        return Response::sendData(true);
    }

    /**
     * @api {post} room/contribution/config/update 房间贡献值配置-修改
     * @apiVersion 0.1.0
     * @apiName 房间贡献值配置-修改
     * @apiGroup 房间贡献值
     * @apiParam {number} id ID,必传
     * @apiParam {number} level 等级,必传
     * @apiParam {string} name 名称,必传
     * @apiParam {string} icon 图标,必传
     * @apiParam {number} value 贡献值,必传
     * @apiSuccessExample 成功响应
     * {
     * "code": 200,
     * "data": true
     * }
     */
    public function update(Request $request)
    {
        //参数校验
        $v = app('validator')->make(
            $request->all(),
            [
                'id'    => 'required|numeric',
                'level' => 'required|numeric',
                'name'  => 'required',
                'icon'  => 'required',
                'value' => 'required|numeric',
            ],
            [
                'level.required' => '等级不能为空',
                'name.required'  => '名称不能为空',
                'icon.exists'    => '图标不能为空',
                'value.numeric'  => '贡献值必须为数字',
            ]);
        if ($v->fails()) {
            throw new BasicException(10001, $v->errors()->first());
        }

        $data = $request->only(['level', 'name', 'icon', 'value']);
        $id = $request->input('id');
        $data['update_time'] = Carbon::now()->timestamp;

        if (app('model_room_contribution_config')->where('id', '<>', $id)->where('level',
                $data['level'])->count() > 0) {
            throw new BasicException(0, '等级已存在');
        }

        app('model_room_contribution_config')->where('id', $id)->update($data);

        app('redis')->del('room:contribution:config');

        return Response::sendData(true);
    }

    /**
     * @api {delete} room/contribution/config 房间贡献值配置-删除
     * @apiVersion 0.1.0
     * @apiName 房间贡献值配置-删除
     * @apiGroup 房间贡献值
     * @apiParam {string} ids ids,多个用英文逗号隔开,必传
     * @apiSuccessExample 成功响应
     * {
     * "code": 200,
     * "data": true
     * }
     */
    public function delete(Request $request)
    {
        $ids = $request->input('ids');
        $param = explode(',', $ids);
        if (!$param) {
            throw new BasicException(0, '请选择要删除的记录');
        }
        app('model_room_contribution_config')->whereIn('id', $param)->delete();

        return Response::sendData(true);
    }

    /**
     * @api {get} room/contribution/config/detail 房间贡献值配置-详情
     * @apiVersion 0.1.0
     * @apiName 房间贡献值配置-详情
     * @apiGroup 房间贡献值
     * @apiSuccessExample 成功响应
     * {
     * "code": 200,
     * "data": {
     * "id": 1,
     * "level": 1,
     * "name": "等级1",
     * "value": 100,
     * "icon": "http://yutangyuyin1.oss-cn-hangzhou.aliyuncs.com/newadmin/e78dcbe611658691758b9f789a821aadKxXca.png",
     * "add_time": "2021-07-13 11:42:29",
     * "update_time": ""
     * }
     * }
     */
    public function detail(Request $request)
    {
        $id = $request->input('id', 0);

        $detail = app('model_room_contribution_config')->where('id', $id)->first();
        if (!$detail) {
            throw new BasicException(10008);
        }
        $detail->add_time = $detail->add_time > 0 ? date('Y-m-d H:i:s', $detail->add_time) : '';
        $detail->update_time = $detail->update_time > 0 ? date('Y-m-d H:i:s', $detail->update_time) : '';

        return Response::sendData($detail);
    }
}
